<?php
/**
 *
 *
 * @package Admin
 */

if ( !defined( 'BIGCKOOL_VERSION' ) ) {
	header( 'HTTP/1.0 403 Forbidden' );
	die;
}


if( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}



class User_List_Table extends WP_List_Table {
	var $post_data = array();
	function __construct(){
	    global $status, $page,$bigckool_admin;
	 
	        parent::__construct( array(
	            'singular'  => __( 'book', 'mylisttable' ),     //singular name of the listed records
	            'plural'    => __( 'books', 'mylisttable' ),   //plural name of the listed records
	            'ajax'      => false        //does this table support ajax?
	 
	    ) );	
	    
	    $this->get_data();
	    
	}
	function get_data(){
		//get data not in top 20:
		$args = array(
					 		
							'orderby' => 'id',
							'order' => 'DESC'							
						);								
		$users= get_users($args);
		$postid=0;			
		if(isset($_GET['postid']))$postid=intval($_GET['postid']);
		
		foreach($users as $k=>$user){		
			$userdata=array();
			$userdata['ID']=$user->ID;
			$vote=get_post_meta($postid,'vote',1);
			
			if(!empty($vote) && array_key_exists($user->ID, $vote)){				
				$bd=get_user_meta($user->ID,'birthday',1);
				$userdata['display_name']=$user->data->display_name;
				$userdata['user_email']=$user->data->user_email;
				$userdata['gender']=get_user_meta($user->ID,'gender',1);	
				$userdata['link']=get_user_meta($user->ID,'link',1);
				$userdata['birthday']=mktime(0,0,0,substr($bd,0,2),substr($bd,3,2),substr($bd,6,2));
				$userdata['location']=get_user_meta($user->ID,'location',1);
				$userdata['clickdate']=$vote[$user->ID]['time'];
				
				if(!empty($vote) && count($vote)>0){
					$userdata['vote']=$vote;
				}	
				
				$this->post_data[]=$userdata;
			}	
		}
		//t($this->post_data);
		
	}
	

 
  function no_items() {
    _e( 'No data found, dude.' );
  }
 
  function column_default( $item, $column_name ) {
    switch( $column_name ) { 
        case 'display_name':
        case 'user_email':
        case 'gender':
        case 'location':
        	return $item[$column_name];
        case 'birthday':
        	return date('d/m/Y',$item[$column_name]);
        case 'clickdate':
        	return date('d/m/Y h:i:s',$item[$column_name]);
        
        case 'link':
            return '<a href="'.$item[$column_name].'">'.$item[$column_name].'</a>';
        default:
            return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes
    }
  }
 
function get_sortable_columns() {
  $sortable_columns = array(
    'display_name'  => array('display_name',false),
  	'clickdate' => array('clickdate',false),
    'user_email' => array('user_email',false),
    'gender'   => array('gender',false),
  	'link' => array('link',false),
  	'birthday' => array('birthday',false),
  	
  	'location' => array('location',false),
  
  );
  return $sortable_columns;
}
 
function get_columns(){
        $columns = array(
            'cb'        => '<input type="checkbox" />',        	
            'display_name' => __( 'Tên', 'mylisttable' ),
        	'clickdate'      => __( 'Ngày bình chọn', 'mylisttable' ),
            'user_email'    => __( 'Email', 'mylisttable' ),
            'gender'      => __( 'Giới tính', 'mylisttable' ),
        'link'      => __( 'FB link', 'mylisttable' ),
        'birthday'      => __( 'Ngày sinh', 'mylisttable' ),
        'location'      => __( 'Location', 'mylisttable' )
        );
         return $columns;
    }
 
function usort_reorder( $a, $b ) {
  // If no sort, default to title
  
  $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'post_title';
  // If no order, default to asc
  $order = ( ! empty($_GET['order'] ) ) ? $_GET['order'] : 'asc';
  // Determine sort order
  
  $result = strcmp( $a[$orderby], $b[$orderby] );
  // Send final sort direction to usort
  return ( $order === 'asc' ) ? $result : -$result;
}
 


 /*
function get_bulk_actions() {
  $actions = array(
    'delete'    => 'Delete'
  );
  return $actions;
}
 */
function column_cb($item) {
        return sprintf(
            '<input type="checkbox" name="book[]" value="%s" />', $item->ID
        );    
    }
 
function prepare_items() {
  $columns  = $this->get_columns();
  $hidden   = array();
  $sortable = $this->get_sortable_columns();
  $this->_column_headers = array( $columns, $hidden, $sortable );
  usort( $this->post_data, array( &$this, 'usort_reorder' ) );
  
  $per_page = $this->get_items_per_page('post_per_page', 5);
  $current_page = $this->get_pagenum();
  $total_items = count( $this->post_data );
 
  // only ncessary because we have sample data
  $this->found_data = array_slice( $this->post_data,( ( $current_page-1 )* $per_page ), $per_page );
 
  $this->set_pagination_args( array(
    'total_items' => $total_items,                  //WE have to calculate the total number of items
    'per_page'    => $per_page                     //WE have to determine how many items to show on a page
  ) );
  $this->items = $this->found_data;
}

}
$posttitle='';
if(isset($_GET['postid'])){
	$postid=intval($_GET['postid']);
	$post=get_post($postid);
	
	if(!empty($post))
		$postitle=' cho bài dự thi <b>'.$post->post_title.'</b>';
}
$myListTable = new User_List_Table();
echo '<div class="wrap"><h2>Danh sách User bình chọn '.$postitle.'</h2>'; 
  $myListTable->prepare_items(); 
  $myListTable->display(); 
  ?>
  <form method="post">
  <input type="hidden" name="page" value="bigckool_dashboard" />
  <?php $myListTable->search_box('search', 'search_id'); ?>
</form>
</div>

